<?php
/**
 * Created by PhpStorm.
 * User: mac
 * Date: 16/8/2
 * Time: 下午10:55
 */

namespace app\admin\controller;


use think\console\Input;
use think\Controller;
use think\Request;
use think\Session;

class User extends Controller
{
    public function __construct(Request $request)
    {
        parent::__construct($request);
        if (!stristr($_SERVER['REQUEST_URI'],"/login")){
            if (!Session::get("login")){
                $this->error("请登录","User/login");
            }
        }
    }

    public function login(){
        $this->view->engine->layout(false);
        return view('login');
    }

    public function user_list(){
        $user = new \app\admin\model\User();
        $users = $user::all();
        $us = [];
        foreach ($users as $u){
            if ($u['mobile']){
                $us[] = $u;
            }
        }
        $this->assign("users",$us);
        return view();
    }

    public function user_oper(){
        $user = new \app\admin\model\User();
        //禁用
        if (Request::instance()->param('oper')==1 && Request::instance()->param('id')){
            $rs = $user::update(['status'=>0],['id'=>Request::instance()->param('id')]);
        //删除
        }else if (Request::instance()->param('oper')==2 && Request::instance()->param('id')){
            $rs = $user::destroy(Request::instance()->param('id'));
        }
        if ($rs){
            $this->success("操作成功");
        }
    }

    public function logout(){
        Session::set('login',false);
        $this->success("注销成功",'user/login');
    }

    public function logindo(){
        $captcha = Request::instance()->post('captcha');
        if(!captcha_check($captcha)){
            $this->error("验证码输入错误");
        };

        $username = Request::instance()->post('user_name');
        $password = Request::instance()->post('password');
        if ($username == 'admin' && $password == 'admin'){
            Session::set('login',true);
            $this->success("登陆成功",'index/index');
        }else{
            $this->error("登录失败,账号或密码错误",'user/login');
        }
    }

    public function captcha(){

    }

    public function adduser(){
        $this->error("后台不开放注册,请直接使用移动端注册");
    }


}